// packagBuy/pages/found/encyclopediaList/encyclopediaList.js
import { foundApi } from '../../../../config/config'
const getURL = require('../../../../utils/util')   //引入配置接口文件
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryId:'',//类目ID
    categoryName:'',//类目名称
    statusBarHeight: app.globalData.statusHeight,
    listData: [],
    page: 1,
    rows: 8,
    nomoreData: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      categoryId:options.id || '',
      categoryName:options.name || ''
    })
    this.getListData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.nomoreData) {
      this.setData({
        page: this.data.page + 1
      })
      this.getListData()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },// 获取优惠券列表数据
  getListData() {
    let params = {
      page: this.data.page,
      rows: this.data.rows
    }
    const categoryId=this.data.categoryId
    const apiUrl=`${foundApi.queryEncyclopediaList}${categoryId}`
    app.reqFun.requestForm('POST',apiUrl, params, (response) => {
      if(response.data){
        const res=response.data
        if (res.success == 1 && res.data) {
          const records = res.data.records || []
          records.forEach(v=>{
            v.pic=getURL.getImgUrl(v.pic)
          })
          this.setData({
            listData: this.data.page === 1 ? records : this.data.listData.concat(records),
            nomoreData: records.length < this.data.rows
          })
        }
      }
    })
  },
})